﻿var getUserID = function(){
	try{
		debugger;
		var userid = sessionStorage.getItem('userid');
		if(userid!=undefined){
			return userid;
		}else{
			window.location.href = '/';
		}
		
	}catch(e){
		alert('登录信息过期，请重新登录');
		window.location.href = '/';
	}
}
var logout = function(){
	sessionStorage.clear();
	window.location.href = '/';
}
// 显示dialog
var dialogShow = function(noteID,content){
	//显示dialog
	$("#opacity").css("display","block");
	document.getElementById("editDiv").setAttribute("style","display:block");
	if(noteID==undefined||noteID==""){
		content = '';
	}
	var editArea = document.getElementById("editArea");
	var save = document.getElementById('save');
	
	editArea.value = content;
	save.onclick = function(){
		saveNote(noteID,editArea.value,getUserID(),function(result){
			debugger;
			if(!result.result){
				alert('保存失败，请重试！！');
			}
		});
		refreshNote();
		dialogHidden();
	}
};
//隐藏dialog
var dialogHidden = function(){
	$("#opacity").css("display","none");
	document.getElementById("editDiv").setAttribute("style","display:none");
}
//保存数据
var saveNote = function(content){
	dialogHidden();
}

//刷新显示每一则日记
var refreshNote = function(){
	var mynote = document.getElementsByName('mynote');
	for(var j=0;j<mynote.length;j++){
		var item0 = mynote[j];
		item0.setAttribute('style','visibility:display');
	}
	
	//从数据库中获取日记信息
	var userid = getUserID();
	var noteData = getNote(userid,function(result){
		
		if(result.result){
			//获取note数据
			var noteData = result.data;
			
			for(var i=0;i<noteData.length;i++){
				if(i>=mynote.length){
					break;
				}
				debugger
				var item = mynote[i];
				item.setAttribute('style','visibility:visible');
				//item.setAttribute('id',noteData[i].noteID);
				item.childNodes[0].innerHTML = noteData[i].datetime;

				item.childNodes[1].setAttribute('id',noteData[i].noteID);	
				item.childNodes[2].textContent = noteData[i].content;
				item.childNodes[2].setAttribute('id',noteData[i].noteID);	
				
				//删除店家事件
				item.childNodes[1].onclick = function(){
					if(confirm('是否删除该条记录？？')){
						deleteNote(this.id,function(result){
							if(!result.result){
								alert('删除失败，请重试！！');
							}else{
								refreshNote();
							}
						});
					}
					
				}
				item.childNodes[2].onclick = function(){
					
					dialogShow(this.id,this.textContent);
				} 
				
			}
		}

	});
	
		

}